<?php
require_once __dir__.'/../../common.php';
ini_set('memory_limit', '1000M');
define('REC_SIZE', 3);
define('SCALE', 40);
define ('SAMPLE_BORDER_SIZE', 200);

function get_file_list($dir)
{
	$ret_arr = array();
	
	if ($handle = opendir($dir)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..") {
				$ret_arr[] = "$dir/$file";
			}
		}
		closedir($handle);
	}
	
	return $ret_arr;
}

function get_image_from_file($filename)
{
	$image_info = getimagesize($filename);
	if (empty($image_info) || !$image_info[2])
		return false;
	
	$image_type = $image_info[2];
	if( $image_type == IMAGETYPE_JPEG ) {
		return imagecreatefromjpeg($filename);
	} elseif( $image_type == IMAGETYPE_GIF ) {
		return imagecreatefromgif($filename);
	} elseif( $image_type == IMAGETYPE_PNG ) {
		return imagecreatefrompng($filename);
	}
}

function rgb2safe($r, $g, $b)
{
	$ret_arr = array();
	$a = array(0, 51, 102, 153, 204, 255);
	foreach ($a as $v)
	{
		if (abs($r-$v) <= 25)
		{
			$ret_arr[0] = $v;
		}
		if (abs($g-$v) <= 25)
		{
			$ret_arr[1] = $v;
		}
		if (abs($b-$v) <= 25)
		{
			$ret_arr[2] = $v;
		}
	}
	
	return $ret_arr;
}

function ImageTrueColorToPalette2( $image, $dither, $ncolors )
{
	$width = imagesx( $image );
	$height = imagesy( $image );
	$colors_handle = ImageCreateTrueColor( $width, $height );
	ImageCopyMerge( $colors_handle, $image, 0, 0, 0, 0, $width, $height, 100 );
	ImageTrueColorToPalette( $image, $dither, $ncolors );
	ImageColorMatch( $colors_handle, $image );
	ImageDestroy( $colors_handle );
}

function hex2rgb($hex)
{
	return array(
		hexdec($hex[0].$hex[1]),
		hexdec($hex[2].$hex[3]),
		hexdec($hex[4].$hex[5]),
		);
}

function hex2safe($hex)
{
	list($r, $g, $b) = hex2rgb($hex);
	list($s_r, $s_g, $s_b) = rgb2safe($r, $g, $b);
	return sprintf("%02x%02x%02x", $s_r, $s_g, $s_b);
}


function draw_color_cube_bak($top_colors, $width, $height, $draw_side=RIGHT_SIDE)
{
	if (empty($top_colors)) return false;
	foreach ($top_colors as $color => $count)
	{
		//echo $count.", ".array_sum($top_colors);
		break;
	}
	
	$draw_percent = $count/array_sum($top_colors);
	$inner_array = array_slice($top_colors, 1, null, true);
	
	switch ($draw_side)
	{
		case LEFT_SIDE:
			$inner_html = draw_color_cube_bak($inner_array,
					$width*(1-$draw_percent),
					$height, LEFT_SIDE);
			return sprintf("<table width=%d height=%d><tr><td width=%d bgcolor=#%s></td><td width=%d>%s</td></tr></table>",
				$width, $height, $width*$draw_percent, $color, $width*(1-$draw_percent), $inner_html);
			break;
		case RIGHT_SIDE:
			$inner_html = draw_color_cube_bak($inner_array,
					$width*(1-$draw_percent),
					$height, RIGHT_SIDE);
			return sprintf("<table width=%d height=%d><tr><td width=%d>%s</td><td width='%d' bgcolor='#%s'></td></tr></table>",
				$width, $height, $width*(1-$draw_percent), $inner_html, $width*$draw_percent, $color);
			break;
		case UP_SIDE:
			$inner_html = draw_color_cube_bak($inner_array,
					$width,
					$height*(1-$draw_percent), UP_SIDE);
			return sprintf("<table width=%d height=%d><tr><td width=%d height=%d bgcolor=#%s></td></tr>"
					."<tr><td height=%d>%s</td></tr></table>",
				$width, $height, $width, $height*$draw_percent, $color, $height*(1-$draw_percent), $inner_html);
			break;
		case DOWN_SIDE:
			$inner_html = draw_color_cube_bak($inner_array,
					$width,
					$height*(1-$draw_percent), DOWN_SIDE);
			return sprintf("<table width=%d height=%d><tr><td height=%d>%s</td></tr><tr><td width=%d height=%d bgcolor=#%s></td></tr></table>",
				$width, $height, $height*(1-$draw_percent), $inner_html, $width, $height*$draw_percent, $color);
			break;
	}
}
